<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />    
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>
    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <div class="header-title">
            我的优惠券
        </div>
        <div class="header-r aui-pull-right">
        </div>
    </header>
    <div style="height:4rem;"></div>

    <ul class="distribution-withdraw-nav">
        <li id="all" class="active" onclick="handleCoupon('all')">全部</li>
        <li id="notuse" onclick="handleCoupon('notuse')">未过期</li>
        <li id="expired" onclick="handleCoupon('expired')">已过期</li>
    </ul>
    <div id="list">
    </div>
    <div class="coupon-t">
        <i class="fa fa-gift"></i>
        赶紧去领券中心看看更多优惠券~
    </div>
</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="coupon">
        <div class="coupon-l">
            ￥<span>{{=value.money}}</span>
            <p>{{=value.name}}</p>
        </div>
        <div class="coupon-m"></div>
        <dl class="coupon-r" tapmode onclick="coupondetails()">
            <dt>
                消费满{{=value.meet_money}}立减
                <p></p>
            </dt>
            <dd>
                <p></p>
                有效期：{{=value.start_time}}至{{=value.end_time}}
            </dd>
        </dl>
        <div class="coupon-btn">立即使用</div>
    </div>
    {{~}}
</script>
<script type="text/javascript">
    apiready = function(){
        globalType = 0;
        loadCouponList();
    }
    
    // 加载优惠券
    function loadCouponList(){

        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Coupon/member',
            method: 'get',
            data: {
                values: {
                    limit: limit,
                    type: globalType,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                var data = ret.result.data;
                for (var i = 0; i < data.length; i++) {
                    data[i].start_time = toDate(data[i].start_time);
                    data[i].end_time = toDate(data[i].end_time);
                }
                templateFun(data);
            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                var data = new Array();
                templateFun(data);
                return false;
            }
        });
    }

    // 模板赋值函数
    function templateFun(data){
        if (data.length) {
            var template = $api.byId('template');
            var dotFn = doT.template(template.innerHTML);
            var html = dotFn(data);
            var list = $api.byId('list');
            $api.html(list, html);
        }else{

          var list = $api.byId('list');
          var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:1rem;">暂无优惠券</h2>';
          list.innerHTML = html;
      }
  }

    // 字符串转时间
    function toDate(timestamp){
            // 宁哥提供代码
            // var newDate = new Date();
            // newDate.setTime(timestamp * 1000);
            // return newDate.toLocaleDateString();
            
            //时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var date = new Date(timestamp * 1000);
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            var D = date.getDate() + ' ';
            var h = date.getHours() + ':';
            var m = date.getMinutes() + ':';
            var s = date.getSeconds();
            return Y+M+D+h+m+s;
            // return Y+M+D;
        }

    // 点击分类
    function handleCoupon(type){

        $('li').removeClass('active');
        $('#'+type).addClass('active');

        if (type == 'all') {
            globalType = 1;
        }else if(type == 'notuse'){
            globalType = 1;
        }else{
            globalType = 2;
        }

        loadCouponList();
    }

    // 优惠券详情
    function coupondetails(){
        var delay = 0;
        if(api.systemType != 'ios'){
            delay = 300;
        }
        api.openWin({
            name: 'coupondetails',
            url: './coupon_details.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
                type:"page"
            }
        });
    };
    
    function userindex(){
        var delay = 0;
        if(api.systemType != 'ios'){
            delay = 300;
        }
        api.openWin({
            name: 'userindex',
            url: './user-index.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
                type:"page"
            }
        });
    };
    function closeWin(){
        api.closeWin({
        });
    }
</script>
</html>